package de.hm.edu.kinderfinder.activities;

import java.util.List;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import de.hm.edu.kinderfinder.location.CircleOverlay;
import de.hm.edu.kinderfinder.location.MapOverlayItems;
import de.hm.edu.kinderfinder.pojos.LocationObject;

public class ZoneConfigView extends MapActivity implements OnTouchListener,
		OnLongClickListener, OnSeekBarChangeListener {

	private MapOverlayItems itemizedoverlay;
	private List<Overlay> mapOverlays;
	private MapView mapView;
	private CircleOverlay circle;
	private static boolean isRunning;

	public static boolean isRunning() {
		return isRunning;
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.isRunning = true;
		setContentView(R.layout.zoneconfigmap);
		mapView = (MapView) findViewById(R.id.zoneview);
		mapView.setOnTouchListener(this);
		mapView.setOnLongClickListener(this);

		mapView.setBuiltInZoomControls(true);
		mapView.setClickable(true);
		mapView.setEnabled(true);
		mapView.setScrollbarFadingEnabled(true);

		SeekBar seekbar = (SeekBar) findViewById(R.id.seekBar);
		seekbar.setOnSeekBarChangeListener(this);

		// mapView.set
		mapOverlays = mapView.getOverlays();
		Drawable drawable = this.getResources().getDrawable(
				android.R.drawable.ic_dialog_info);
		itemizedoverlay = new MapOverlayItems(drawable, this);

		GeoPoint point = new GeoPoint(19240000, -99120000);
		OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!",
				"I'm in Mexico City!");

		// read loc from DB
		SharedPreferences prefs = getSharedPreferences("Location", MODE_PRIVATE);
		float latitude = prefs.getFloat("latitude", 0);
		float longitude = prefs.getFloat("longitude", 0);

		GeoPoint point2 = new GeoPoint((int) (longitude * 1E6),
				(int) (latitude * 1E6));

		OverlayItem overlayItemDebug = new OverlayItem(point2, "Klein Stefan",
				"Hat sich verlaufen!");
		itemizedoverlay.addOverlay(overlayItemDebug);

		itemizedoverlay.addOverlay(overlayitem);
		mapOverlays.add(itemizedoverlay);

	}

	@Override
	public void onNewIntent(Intent newIntent) {
		// update map
		super.onNewIntent(newIntent);
		LocationObject dataObj = (LocationObject) newIntent.getExtras()
				.getSerializable("Data");

		GeoPoint point2 = new GeoPoint((int) (dataObj.getAtt() * 1E6),
				(int) (dataObj.getLatt() * 1E6));

		OverlayItem overlayitem2 = new OverlayItem(point2, "Klein Stefan",
				"Hat sich verlaufen!");
		itemizedoverlay.addOverlay(overlayitem2);
		// mapOverlays.add(itemizedoverlay);
		itemizedoverlay.populateOverlays();
	}

	@Override
	protected void onPause() {
		super.onPause();
		isRunning = false;
	}

	@Override
	protected void onResume() {
		super.onResume();
		isRunning = true;
	}

	@Override
	public boolean onTouch(View arg0, MotionEvent event) {
		// TODO Auto-generated method stub
		int action = event.getAction();
		final int x = (int) event.getX();
		final int y = (int) event.getY();

		GeoPoint pt = mapView.getProjection().fromPixels(x, y);
		OverlayItem overlayItemDebug = new OverlayItem(pt, "Klein Stefan",
				"Hat sich verlaufen!");
		itemizedoverlay.addOverlay(overlayItemDebug);

		return false;
	}

	public void zoneconfig(View v) {
		Intent i = new Intent(this, ZoneConfigView.class);
		startActivity(i);

	}

	@Override
	public boolean onLongClick(View arg0) {
		return false;
		// Date d= new Date();
		// Calendar c=Calendar.getInstance();
		// c.get(Calendar.D)
	}

	@Override
	public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
		circle.setRadius(arg1 * 10);
		itemizedoverlay.populateOverlays();
		Log.i("pk", "on bar changed " + arg1);

	}

	@Override
	public void onStartTrackingTouch(SeekBar arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStopTrackingTouch(SeekBar arg0) {
		// TODO Auto-generated method stub

	}
}
